package com.lahuan.export_import;

import com.lahuan.demo.entity.Order;
import com.lahuan.demo.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.LinkedList;
import java.util.List;


@ActiveProfiles("hikari")
@Slf4j
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = BuildMysqlData.class)
@SpringBootApplication(scanBasePackages = "com.lahuan")
public class BuildMysqlData {

    @Autowired
    OrderService orderService;


    @Test
    public void test() {

        long insertStart = System.currentTimeMillis();
        List<Order> batchList = new LinkedList<>();
        for (int i = 0; i < 10_000_000; i++) {
            Order o = new Order();
            o.setOrderSn("order_"+i);
            batchList.add(o);
            if(batchList.size()>100000){
                orderService.saveBatch(batchList);
                batchList.clear();
                log.info("当前进度:"+i);
            }
        }
        if(batchList.size()>0){
            orderService.saveBatch(batchList);
            batchList.clear();
        }
        long insertEnd = System.currentTimeMillis();
        long insertCost= insertEnd - insertStart;
        System.out.println("mysql cost:"+insertCost);
    }
}
